package com.heatup.apt.common.dao.redis;

import com.heatup.apt.common.constants.TokenConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.heatup.apt.common.dao.TokenMapper;
import com.heatup.apt.model.weixin.Token;
@Component("tokenRedisDao")
public class TokenRedisDao extends BaseRedisDao<Token> {
	private final static Logger logger = LoggerFactory.getLogger(TokenRedisDao.class);
	
	@Autowired
	private TokenMapper tokenMapper;
	
	/**
	 * 获取Token,若果redis不存在，则从缓存中取
	 * @return
	 */
	public Token queryTokenByRedis() {
		Token token = null;
		logger.info("==>> 从redis获取token");
		try {
			token = this.get(TokenConstants.ACCESS_TOKEN_KEY, Token.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("==>>TokenMapper queryTokenByRedis 从redis获取token异常：" + e.getMessage());
		}
		if (token != null) {
			logger.info("=====>>从redis获取token完成");
			return token;
		} else {
			logger.info("====>>未从redis获取token,开始查询数据库信息");
			token = tokenMapper.selectTokenByKey(TokenConstants.ACCESS_TOKEN_KEY);
			return token;
		}
	}
}
